package model;

import entities.Shift;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class EditShiftTableModel extends AbstractTableModel {

    ArrayList<Shift> list;

    public EditShiftTableModel() {
        list = new ArrayList<Shift>();
    }

    public EditShiftTableModel(ArrayList<Shift> list) {
        this.list = list;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Shift shift = list.get(rowIndex);
        Date startTime = shift.getStartTime();
        Date endTime = shift.getEndTime();
        switch (columnIndex) {
            case 0:
                return shift.getShiftID();
            case 1:
                return shift.getName();
            case 2:
                return startTime;
            case 3:
                return endTime;
            case 4:
                return shift.getModifiedDate();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Integer.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return String.class;
            case 4:
                return Date.class;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Shift";
            case 2:
                return "StartTime";
            case 3:
                return "EndTime";
            case 4:
                return "ModifiedDate";
            default:
                return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public boolean addAll(ArrayList<Shift> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public boolean addElement(Shift shift) {
        boolean returnVal = list.add(shift);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public Shift getAt(int index) {
        return list.get(index);
    }

    public void removeAt(int index) {
        list.remove(index);
        fireTableDataChanged();
    }
    
    public void remove(Shift shift) {
        list.remove(shift);
        fireTableDataChanged();
    }        

    public void update(int index, Shift shift) {
        list.set(index, shift);
        fireTableDataChanged();
    }
    
    public void update(Shift shift) {
        list.set(list.indexOf(shift), shift);
        fireTableDataChanged();
    }

    public void clear() {
        list.clear();
    }

    public boolean containtID(int id) {
        for (Shift record : list) {
            if (record.getShiftID() == id) {
                return true;
            }
        }
        return false;
    }
    
    public boolean containtName(String name) {
        for (Shift record : list) {
            if (record.getName().equalsIgnoreCase(name)) {
                return true;
            }
        }
        return false;
    }
}
